<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="getList"
  >
    <!-- @load="getList" -->
    <div class="lsit_box">
      <div class="row">
        <div class="row0" ref="row0">
          <van-cell v-for="item in list[0]" :key="item.msg">
            <img :src="item.img_url" alt="" />
            <p>{{ item.msg }}</p>
            <span>{{ item.date }}</span>
          </van-cell>
        </div>
      </div>
      <div class="row">
        <div ref="row1">
          <van-cell v-for="item in list[1]" :key="item.msg">
            <img :src="item.img_url" alt="" />
            <p>{{ item.msg }}</p>
            <span>{{ item.date }}</span>
          </van-cell>
        </div>
      </div>
    </div>
  </van-list>
</template>

<script>
import { getData } from "@/apis";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
    };
  },
  methods: {
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        let index = 0;
        let row = 2;

        let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        data.map(() => {
          this.list[index]
            ? this.list[index].push(this.list[index].length + 1)
            : (this.list[index] = [0]);

          // 瀑布流实现
          if (index >= row - 1) index = 0;
          else index++;
        });

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 40) {
          this.finished = true;
        }
      }, 1000);
    },
    getList() {
      getData(10).then(async ({ list }) => {
        for (let i = 0; i < list.length; i++) {
          let index = await this.getPushIndex();
          this.list[index]
            ? this.list[index].push(list[i])
            : this.$set(this.list, index, [list[i]]);
        }

        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list[0].length >= 30) {
          this.finished = true;
        }
      });
    },
    getPushIndex() {
      return new Promise((res) => {
        this.$nextTick(() => {
          let refs = this.$refs;
          res(
            refs[`row${0}`].offsetHeight <= refs[`row${1}`].offsetHeight ? 0 : 1
          );
        });
      });
    },
  },
};
</script>

<style>
.lsit_box {
  display: flex;
}
.lsit_box .row {
  flex: 1;
}
</style>
